﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace DotNetGPSTest
{
   public class OSTileProvider : TileProvider
   {
      LatLongToOSGB m_converter = new LatLongToOSGB();

      public MapSpec m_spec =
         new MapSpec(200, 1000,
            "C_", "\\Storage Card\\Maps\\200PIX", ".gif");

      // Returns tile at this position.
      public TileInfo getTileAt(GPSPosition position)
      {
         TileInfo info = new TileInfo();
         info.Filename = getBitmapFilenameAt(toGridRef(position));
         info.HeightPixels = m_spec.m_tileWidthPixels; // Width and height same at present
         info.WidthPixels = m_spec.m_tileWidthPixels;
         info.RequestedPosition = position;
         info.RequestedTileOffset = new Point(0, 0);
         info.Offset = getTileTopLeftOffset(toGridRef(position));
         info.Zoom = 1; // TODO: Need to handle multi zoom levels
         return new TileInfo();
      }

      // Returns the tile at the offset index of the one centered at position.
      // xIndex=0, yIndex=1 is tile adjacent below tile at 'position'.
      // xIndex=-1, yIndex=-1 is tile top left of tile at 'position'.
      public TileInfo getTileAt(GPSPosition position, int xIndex, int yIndex)
      {
         return new TileInfo();
      }
      // Returns the amount of pixels between A and B.
      // Negative x B is left of A
      // Negative y B is above A
      public Point getOffset(GPSPosition positionA, GPSPosition positionB)
      {
         return new Point();
      }

      public Point toGridRef(GPSPosition position)
      {
         m_converter.lati_ = position.Latitude;
         m_converter.longi_ = position.Longitude;
         m_converter.Convert();
         return new Point(m_converter.e, m_converter.n);
      }

      public string getBitmapFilenameAt(Point gridRef)
      {
         int botLeftTileE = (gridRef.X / m_spec.m_tileWidthMetres) * m_spec.m_tileWidthMetres;
         int botLeftTileN = (gridRef.Y / m_spec.m_tileWidthMetres) * m_spec.m_tileWidthMetres;
         string easting = botLeftTileE.ToString("D7");
         string northing = botLeftTileN.ToString("D7");
         string folderE = easting.Substring(0, 2);
         string folderN = northing.Substring(0, 2);
         string folder = m_spec.m_folderPrefix + folderE + folderN;
         string file = easting.Substring(1, 4) + northing.Substring(1, 4) + m_spec.m_fileExt;
         string filename = m_spec.m_mapsFolder + "\\" + folder + "\\" + file;
         return filename;
      }

      public Point getTileTopLeftOffset(Point gridRef)
      {
         int botLeftTileE = (gridRef.X / m_spec.m_tileWidthMetres) * m_spec.m_tileWidthMetres;
         int botLeftTileN = (gridRef.Y / m_spec.m_tileWidthMetres) * m_spec.m_tileWidthMetres;

         double dE = gridRef.X - botLeftTileE;
         double dN = gridRef.Y - botLeftTileN;

         return new Point((int)dE, (int)dN);
      }
   }
}
